#!/usr/bin/env python

import sys, os, re, codecs
import time
import win32com.client

class htmlfile:
    def __init__(self):
        self.__ie = win32com.client.Dispatch('InternetExplorer.Application')
        self.__ie.Silent = True
        self.__filename  = ''
        self.__document  = None
        
    def __del__(self):
        self.__ie.Quit()
        
    def __getdocument(self, filename):
        filename = os.path.abspath(filename)
        if self.__filename != filename:
            self.__filename = filename
            self.__ie.Navigate2(filename)
            self.__ie.Document.close()
            while self.__ie.Document.Body is None:
                time.sleep(0.1)
            self.__document = self.__ie.Document
        return self.__document
    def gettext(self, filename):
        return self.__getdocument(filename).Body.innerText
    def gettitle(self, filename):
        return self.__getdocument(filename).title

def formattextpath(dir, htmlfile, htmltitle):
    '''Format the text file path and return.
    '''
    fname = htmltitle[:6]
    fname = re.sub(r'(\|/|:|*|?|<|>|||")', '-', fname)
    fname = fname + '_' + os.path.splitext(htmlfile)[0] + '.txt'
    return os.path.join(root, fname)

if __name__ == '__main__':
    hf = htmlfile()
    for root, dirs, names in os.walk(u'.'):
        for name in names:
            if name.endswith('htm') or name.endswith('html'):
                htmlpath = os.path.join(root, name)
                textpath = formattextpath(root, name, hf.gettitle(htmlpath))
                print htmlpath, '->', textpath
                file(textpath, 'wb').write(hf.gettext(htmlpath).encode('mbcs'))
            # End of if.
        # End of for name.
    # End of for root.
    del hf
# End of if.
